package com.intramirror.wd.business.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * Created by zhongyu on 2017/12/15.
 */
@Configuration
public class DataSourceConfiguration {
	Logger logger = LoggerFactory.getLogger(DataSourceConfiguration.class);

	@Value("${datasource.type}")
	private Class<? extends DataSource> dataSourceType;

	@Bean(name = "writeDataSource")
	@Primary
	@ConfigurationProperties(prefix = "datasource.write")
	public DruidDataSource writeDataSource() {
		logger.info("-------------------- writeDataSource init ---------------------");

		return new DruidDataSource();
	}

	/**
	 * 有多少个从库就要配置多少个
	 * @return
	 */
	@Bean(name = "readDataSource1")
	@ConfigurationProperties(prefix = "datasource.read1")
	public DruidDataSource readDataSourceOne() {
		logger.info("-------------------- readDataSourceOne init ---------------------");
		return new DruidDataSource();
	}

	@Bean(name = "readDataSource2")
	@ConfigurationProperties(prefix = "datasource.read2")
	public DruidDataSource readDataSourceTwo() {
		logger.info("-------------------- readDataSourceTwo init ---------------------");
		return new DruidDataSource();
	}
}
